package com.weblog.upload.controller;

import com.weblog.upload.service.UploadService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaTray;
import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/upload")
@CrossOrigin
public class UploadController {

    @Autowired
    private UploadService uploadService;
    /**
     * 上传图片
     * @param file
     * @return
     */
    @PostMapping(value = "/image",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result uploadImage(@RequestParam("file")MultipartFile file){

        try {
            String url = uploadService.uploadImage(file);
            return new Result(true, StatusCode.OK,"上传成功",url);
        }catch (Exception e){

            return new Result(false,StatusCode.ERROR,"上传出错");
        }

    }

    /**
     * 文章多文件上传
     * @param files
     * @return
     */
    @PostMapping(value = "/files",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result uploadMultiFiles(@RequestParam("files") MultipartFile[] files){
        try {
            List<String> resultList = uploadService.uploadMultiFiles(files);
            return new Result(true,StatusCode.OK,"上传成功",resultList);
        }catch (Exception e){
            return new Result(false,StatusCode.ERROR,"啊哦~图片上传出现了点小错误，请稍后再上传");
        }


    }
}
